/**
 *
 */
package anysware.file.dao.hibernate;

import java.util.Collection;
import java.util.Date;

import org.hibernate.criterion.Restrictions;

import anysware.core.dao.hibernate.GenericHibernateDao;
import anysware.file.FileItem;
import anysware.file.dao.FileItemDao;
import anysware.source.Source;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
public class FileItemHibernateDao extends GenericHibernateDao<FileItem, Long>
        implements FileItemDao {

	public FileItemHibernateDao() {
		super(FileItem.class);
	}

	@SuppressWarnings("unchecked")
	public Collection<FileItem> getByLastModified(final Date date) {
		return currentSession().createCriteria(clazz).add(
		        Restrictions.ge("lastModified", date)).list();

	}

	public Collection<FileItem> getChangedFileItems() {
		return currentSession().createCriteria(clazz).add(
		        Restrictions
		                .or(Restrictions.isNull("lastAccessed"), Restrictions
		                        .geProperty("lastModified", "lastAccessed")))
		        .list();
	}

	public Collection<FileItem> getChangedFileItems(final Source source) {
		return currentSession().createCriteria(clazz).add(
		        Restrictions.and(Restrictions.eq("source", source),
		                Restrictions.or(Restrictions.isNull("lastAccessed"),
		                        Restrictions.geProperty("lastModified",
		                                "lastAccessed")))).list();
	}

}
